<?php

namespace app\system\controller;

use app\common\model\AuthGroupModel;
use app\common\model\AuthGroupRuleModel;
use think\Request;

class AuthGroupController extends BaseController
{
    private $utype;
    private $authGroupModel;
    private $authRuleCache;

    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->utype = $this->request->param('utype');
        if (empty($this->utype)) {
            $this->error('未指定模块参数');
        }
        $this->authGroupModel = new AuthGroupModel();
        $this->authRuleCache  = cache('auth_rule_'.$this->utype);
    }

    public function index()
    {
        $utypeList = get_dict('101');
        $groupList = $this->authGroupModel->where('utype', $this->utype)->order('group_sort asc')->select();
        $this->assign('utypeList', $utypeList);
        $this->assign('groupList', $groupList);
        return $this->fetch();
    }

    public function add()
    {
        if ($this->request->isAjax()) {
            try{
                $data   = $this->request->param();
                $result = $this->validate($data, [
                    'group_id|权限编码' => 'require',
                    'utype|会员模型' => 'require',
                    'title|角色名称' => 'require',
                    'group_sort|排序' => 'require',
                ]);
                if (true !== $result) {
                    exception($result,40001);
                }
                if($this->authGroupModel->where('group_id',$data['group_id'])->find()){
                    exception('权限编码已存在!',40001);
                }
                $this->authGroupModel->addData($data);
                $authGroupRuleModel = new AuthGroupRuleModel();
                $authGroupRuleModel->addRule($this->authGroupModel->group_id,$data['rule']);
            } catch (\Exception $e){
                $this->error($e->getMessage());
            }
            $this->success('添加成功', url('index', ['utype' => $this->utype]));
        }
        $this->assign('authRule', $this->authRuleCache);
        return $this->fetch();
    }

    public function edit()
    {
        $authGroupRuleModel = new AuthGroupRuleModel();
        if ($this->request->isAjax()) {
            try{
                $data   = $this->request->param();
                $result = $this->validate($data, [
                    'group_id|权限编码' => 'require',
                    'utype|会员模型' => 'require',
                    'title|角色名称' => 'require',
                    'group_sort|排序' => 'require',
                ]);
                if (true !== $result) {
                    exception($result,40001);
                }
                $this->authGroupModel->editData($data);
                $authGroupRuleModel->addRule($data['group_id'],$data['rule']);
            } catch (\Exception $e){
                $this->error($e->getMessage());
            }
            $this->success('修改成功', url('index', ['utype' => $this->utype]));
        }
        $groupId = $this->request->param('group_id/d');
        $info    = $this->authGroupModel->get($groupId);
        $groupRule = $authGroupRuleModel->where('group_id',$groupId)->column('rule_button','rule_id');
        $myrules = array_keys($groupRule);
        $this->assign('myrules', $myrules);
        $this->assign('groupRule', $groupRule);
        $this->assign('authRule', $this->authRuleCache);
        $this->assign('all', 'all');
        $this->assign('info', $info);
        return $this->fetch();
    }

    /**
     * 删除规则
     */
    public function del()
    {
        try{
            $groupId = $this->request->param('group_id/d');
            $this->authGroupModel->where('group_id',$groupId)->delete();
            $authGroupRuleModel = new AuthGroupRuleModel();
            $authGroupRuleModel->where('group_id',$groupId)->delete();
        } catch (\Exception $e){
            $this->error($e->getMessage());
        }
        $this->success('删除成功', url('index', ['utype' => $this->utype]));
    }

    /**
     * 更新排序
     */
    public function updateOrder()
    {
        try{
            $listOrder = $this->request->param('list_order/a');
            foreach ($listOrder as $groupId => $groupSort) {
                $groupId    = intval($groupId);
                $groupSort      = intval($groupSort);
                $dataList[] = ["group_id" => $groupId, "group_sort" => $groupSort];
            }
            $this->authGroupModel->saveAll($dataList);
        } catch (\Exception $e){
            $this->error($e->getMessage());
        }
        $this->success('排序成功！');
    }

    /**
     * 获取当前按钮上下级
     */
    public function getUpDown(){
        $checked = $this->request->param('checked/d');
        $value = $this->request->param('value/d');
        $data['down'] = array_keys(\org\util\TreeUtil::channelList($this->authRuleCache, $value, "&nbsp;", 'rule_id', 'rule_pid'));
        $up = \org\util\TreeUtil::parentChannel($this->authRuleCache, $value, 'rule_id', 'rule_pid');
        $data['up'] = array_column($up,'rule_id');
        $data['checked']=$checked;
        return $data;
    }
}